// pages/mine/order/order_detail.js
var App = getApp();
let status='';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no:'',
    order_id:'',
    order_time:'',
    goodsList:[],
    totalAmount:0,//合计金额
    orderType: 0,//是否是未确认订单
    type:0,
    showConfirm: false, //显示确认收货弹窗
    completeUser:'',
    completeTime:'',
    completePhone:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      no: options.order_no,
      type: options.type
    })
    status = options.type;
    this.getSuplymentDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //back
  backLastPage: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  // 取消订单
  onCancelOrder:function(e){
    let order_no = e.currentTarget.dataset.no;
    let that = this;

    wx.request({
      url: App.data.globalAPI + '/order/supplyorder/cancel',
      data: {
        token: wx.getStorageSync('token'),
        order_no: order_no,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          var pages = getCurrentPages();
          var prePage = pages[pages.length - 2];//触发上个界面的方法 
          prePage.onLoad(); 
          wx.showToast({
            title: '订单取消成功!',
            duration: 800
          })
          that.setData({
            orderType:4
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 800
          })
        }
      }
    })
  },
  //获取补货订单详情
  getSuplymentDetail:function(){
    let that = this;
    let index = that.data.currentTab;
    wx.request({
      url: App.data.globalAPI + '/order/supplyorder/detail',
      data: {
        token: wx.getStorageSync('token'),
        order_no: that.data.no
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          let data = res.data.data
          that.setData({
            no: data.order_no,
            order_id: data.order_no,
            order_time: data.order_time,
            goodsList: data.goods,
            totalAmount: data.price,//合计金额
            orderType: data.order_status,//是否是未确认订单
            completeUser: data.complete_user == '' ? '-' : data.complete_user,
            completeTime: data.complete_time,
            completePhone: data.complete_user_phone
          })
          wx.hideLoading();
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:600
          })
        }
      }
    })
  },

  // 确认送达弹窗
  onConfirmlOrder: function (e) {
    this.setData({
      showConfirm: true
    })
  },

  // 关闭确认送达
  closeSendWin: function () {
    this.setData({
      showConfirm: false
    })
  },

  // 确认送达
  onSendHandle: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/order/users/confirm',
      data: {
        token: wx.getStorageSync('token'),
        order_no: that.data.no,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          that.setData({
            type: 3,
            orderType: 3,
            showConfirm: false,
            completeUser: res.data.data.user_name == '' ? '-' : res.data.data.user_name,
            completeTime: res.data.data.complete_time,
            completePhone: res.data.data.user_phone
          })
          var pages = getCurrentPages();
          var prePage = pages[pages.length - 2];//触发上个界面的方法 
          if (prePage.route =='pages/mine/order/box_order'){
            prePage.onLoad(); 
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 800
          })
        }
      },fail(){
        wx.showToast({
          title: '网络异常，请稍后重试！',
          icon: 'none',
          duration: 800
        })
      }
    })
  }
})